package com.canaan.business.controller.pc.inOutStock;

import java.util.List;
import java.util.Map;
import java.util.stream.Collectors;

import cn.hutool.core.collection.CollectionUtil;
import com.canaan.business.domain.vo.SplitBillDetailResultVo;
import com.canaan.business.domain.vo.SplitBillDetailVo;
import com.canaan.business.domain.vo.TraySplitBillDetailVo;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;

import javax.servlet.http.HttpServletResponse;

import org.springframework.security.access.prepost.PreAuthorize;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;
import com.canaan.common.annotation.Log;
import com.canaan.common.core.controller.BaseController;
import com.canaan.common.core.domain.AjaxResult;
import com.canaan.common.enums.BusinessType;

import javax.annotation.Resource;

import com.canaan.business.domain.inOutStock.SplitBillDetail;
import com.canaan.business.service.inOutStock.ISplitBillDetailService;
import com.canaan.common.utils.poi.ExcelUtil;
import com.canaan.common.core.page.TableDataInfo;

/**
 * 分捡单明细Controller
 *
 * @author canaan
 * @date 2025-03-29
 */
@Api(tags = "分捡单明细")
@RestController
@RequestMapping("/inOutStock/splitBillDetail")
public class SplitBillDetailController extends BaseController {
    @Resource
    private ISplitBillDetailService splitBillDetailService;

    /**
     * 查询分捡单明细列表
     */
    @ApiOperation(value = "查询分捡单明细列表")
//    @PreAuthorize("@ss.hasPermi('inOutStock:splitBillDetail:list')")
    @GetMapping("/list")
    public TableDataInfo list(SplitBillDetail splitBillDetail) {
        startPage();
        List<SplitBillDetailResultVo> list = splitBillDetailService.selectSplitBillDetailListPageData(splitBillDetail);
        return getDataTable(list);
    }

    /**
     * 导出分捡单明细列表
     */
    @ApiOperation(value = "导出分捡单明细列表")
    @PreAuthorize("@ss.hasPermi('inOutStock:splitBillDetail:export')")
    @Log(title = "分捡单明细", businessType = BusinessType.EXPORT)
    @PostMapping("/export")
    public void export(HttpServletResponse response, SplitBillDetail splitBillDetail) {
        List<SplitBillDetail> list = splitBillDetailService.selectSplitBillDetailList(splitBillDetail);
        ExcelUtil<SplitBillDetail> util = new ExcelUtil<SplitBillDetail>(SplitBillDetail.class);
        util.exportExcel(response, list, "分捡单明细数据");
    }

    /**
     * 获取分捡单明细详细信息
     */
    @ApiOperation(value = "获取分捡单明细详细信息")
//    @PreAuthorize("@ss.hasPermi('inOutStock:splitBillDetail:query')")
    @GetMapping(value = "/{id}")
    public AjaxResult getInfo(@PathVariable("id") Long id) {
        return success(splitBillDetailService.selectSplitBillDetailById(id));
    }

    /**
     * 新增分捡单明细
     */
    @ApiOperation(value = "新增分捡单明细")
    @PreAuthorize("@ss.hasPermi('inOutStock:splitBillDetail:add')")
    @Log(title = "分捡单明细", businessType = BusinessType.INSERT)
    @PostMapping
    public AjaxResult add(@RequestBody SplitBillDetail splitBillDetail) {
        return toAjax(splitBillDetailService.insertSplitBillDetail(splitBillDetail));
    }

    /**
     * 修改分捡单明细
     */
    @ApiOperation(value = "修改分捡单明细")
    @PreAuthorize("@ss.hasPermi('inOutStock:splitBillDetail:edit')")
    @Log(title = "分捡单明细", businessType = BusinessType.UPDATE)
    @PutMapping
    public AjaxResult edit(@RequestBody SplitBillDetail splitBillDetail) {
        return toAjax(splitBillDetailService.updateSplitBillDetail(splitBillDetail));
    }

    /**
     * 删除分捡单明细
     */
    @ApiOperation(value = "删除分捡单明细")
    @PreAuthorize("@ss.hasPermi('inOutStock:splitBillDetail:remove')")
    @Log(title = "分捡单明细", businessType = BusinessType.DELETE)
    @DeleteMapping("/{ids}")
    public AjaxResult remove(@PathVariable Long[] ids) {
        return toAjax(splitBillDetailService.deleteSplitBillDetailByIds(ids));
    }


    @ApiOperation(value = "托盘上所有分拣单明细")
    @GetMapping("/getSplitBillDetailsByTray")
    public AjaxResult getSplitBillDetailsByTray(@RequestParam("trayNo") String trayNo, @RequestParam("billType") Integer billType) {
        List<TraySplitBillDetailVo> list = splitBillDetailService.getSplitBillDetailsByTray(trayNo, billType);
        if (CollectionUtil.isNotEmpty(list)) {
            return AjaxResult.success(list);
        } else {
            return AjaxResult.error("未查询到数据");
        }
    }
}
